<template>
	<view class="">
		<view class="">JQL查询练习</view>
		<view class="" @click="updates()">更新记录</view>
		<view class="">
			<t-btn @click="randomMusic()">随机获取</t-btn>
		</view>
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" @load="handload" :where="where"
			collection="my-music-backup">
			<view v-if="error">{{error.message}}</view>
			<view class="m-2" v-else v-for="(item,index) in data" :key="index">
				<text selectable @click="myupdate(item._id)">{{item.title}}-{{item._id}}</text>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				where: "category_id == '61c16aaebdfab50001f3fe61'"
			}
		},
		mounted() {
			// const res = await db.collection('my-music-backup')
			// 	// where 两种写法都可以
			// 	// .where("category_id=='61b3691f39bf100001e7ccc1'")
			// 	.where({
			// 		category_id: '61d6f08b97b9a00001f979b7'
			// 	})
			// 	.field('title,poster')
			// 	.get()
			// console.log('jsAPI获取的数据', res.result.data)
		},
		methods: {
			handload(data) {
				// console.log(data)
			},
			async randomMusic() {
				const db = uniCloud.database()
				const res = await db.collection('my-music-backup').aggregate()
					.match({
						article_status: 1,
						// category_id: '61d6f08b97b9a00001f979b7'
					})
					.sample({
						size: 10
					})
					.end()
				console.log('随机获取的数据', res.result.data)
				this.$refs.udb.dataList = res.result.data
			},
			async myupdate(id) {
				console.log(id)
				await uniCloud.callFunction({
					name: 'music-update',
					data: {
						id
					},
					success(e) {
						console.log(e.result)
					}
				})
			},
			async updates(){
				const db=uniCloud.database()
				const dbCmd = db.command
				const res= await db.collection("my-music-backup").doc('61b37230cd02c3000133898d')
				// .doc('61b37230cd02c3000133898d')   //上面语句查询出的_id
				.update({
						'article_status':'100'
					}).catch((e)=>{
						console.log(e)
					})
				console.log(res)	
			}
		}
	}
</script>

<style>
</style>
